/**
 * author:Administrator
 * date:2016/2/24
 * desc:TODO
 */

'use strict';

let seq = 1;
let list = {};

class TicketRepoImpl {

  static * genId() {
    return 'ticket_' + (seq++);
  }

  static * find(id) {
    return list[id];
  }

  static * findByCode(code) {
    //console.log('all ticket', list);
    //console.log('find ticket,code=%s', code);
    let ids = Object.keys(list);
    for (let id of ids) {
      let item = list[id];
      if (item.code == code) return item;
    }
    return null;
  }

  static *store(ticket) {
    if (!ticket.id) ticket.id = yield TicketRepoImpl.genId();
    list[ticket.id] = ticket;
  }
}
module.exports = TicketRepoImpl;